//
//  StrategyUpViewController.m
//  BeautifulShoppings
//
//  Created by GivenBmli on 16/9/5.
//  Copyright © 2016年 GivenBmli. All rights reserved.
//

#import "StrategyUpViewController.h"
#import "StrategyDetailsTableViewCell.h"
#import "NetworkHandler.h"
#import "StrateDetailsModel.h"
#import "GuideHomeViewController.h"
#import "ToolModel.h"
#import "TAMacro.h"
@interface StrategyUpViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic,strong)UITableView * tableView;
@property (nonatomic,strong)StrategyDetailsTableViewCell * cell;
@property (nonatomic,strong)NSMutableArray * array;
@end
@implementation StrategyUpViewController
- (NSMutableArray *)array{
    if (!_array) {
        self.array = [NSMutableArray array];
    }
    return _array;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    UIButton *butt = [UIButton buttonWithType:UIButtonTypeCustom];
    [butt setImage:[UIImage imageNamed:@"返回"] forState:UIControlStateNormal];
    butt.frame  = CGRectMake(0, 0, 50, 50);
    UIBarButtonItem *left= [[UIBarButtonItem alloc]initWithCustomView:butt];
    [butt addTarget:self action:@selector(clickLeft:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem =left;
    
    
    // NSLog(@"%@",self.str);
    [self analysis];
    [self FoundTableView];
//    [self nightModel];
}


-(void)analysis{
    [NetworkHandler getDataByURLString:[NSString stringWithFormat:@"http://api.liwushuo.com/v2/collections/%@/posts?limit=20&offset=0",self.str] BodyString:nil WithDataBlock:^(id result) {
        NSDictionary *dic = [result objectForKey:@"data"];
        NSMutableArray *arr = [dic objectForKey:@"posts"];
        for (NSDictionary *dic in arr) {
            StrateDetailsModel *model = [StrateDetailsModel modelWithDic:dic];
            [self.array addObject:model];
        }
        [self.tableView reloadData];
    }];
}
-(void)FoundTableView{
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT) style:0];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.array.count;
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *ID = @"cell";
    self.cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (self.cell  == nil) {
        self.cell = [[StrategyDetailsTableViewCell alloc]initWithStyle:0 reuseIdentifier:ID];
    }
    StrateDetailsModel *model = self.array[indexPath.item];
    self.cell.strateDetailsModel = model;
    return self.cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 200;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    self.hidesBottomBarWhenPushed = YES;
    StrateDetailsModel *model = self.array[indexPath.item];
    GuideHomeViewController *guideHomeViewController = [[GuideHomeViewController alloc]init];
    guideHomeViewController.str = [NSString stringWithFormat:@"%@",model.Id];
    ToolModel *tool = [[ToolModel alloc]init];
    tool.title = model.title;
    tool.cover_image_url = model.cover_image_url;
    guideHomeViewController.IMAGE = model.cover_image_url;
    guideHomeViewController.STR = model.title;
    [self.navigationController pushViewController:guideHomeViewController animated:YES];
    self.hidesBottomBarWhenPushed = YES;
}
-(void)clickLeft:(UIBarButtonItem*)qwe{
    [self.navigationController popViewControllerAnimated:YES];
}

//- (void)nightModel {
//    //设置日间和夜间两张状态
//    [self.view jxl_setDayMode:^(UIView *view) {
//        //设置日间模式
//        view.backgroundColor = [UIColor whiteColor];
//        
//    } nightMode:^(UIView *view) {
//        view.backgroundColor = [UIColor colorWithRed:0.12 green:0.14 blue:0.15 alpha:1.00];
//    }];
//    [self.navigationController.navigationBar jxl_setDayMode:^(UIView *view) {
//        UINavigationBar *bar = (UINavigationBar *)view;
//        //改变状态栏前景颜色
//        bar.barStyle = UIBarStyleDefault;
//        bar.barTintColor = [UIColor colorWithRed:0.98 green:0.29 blue:0.31 alpha:1.00];
//        //        bar.barTintColor = [UIColor redColor];
//    } nightMode:^(UIView *view) {
//        UINavigationBar *bar = (UINavigationBar *)view;
//        //改变状态栏前景颜色
//        bar.barStyle = UIBarMetricsDefault;
//        bar.barTintColor = [UIColor colorWithRed:0.12 green:0.14 blue:0.15 alpha:1.00];
//    }];
//    
//    
//    
//    self.view.backgroundColor = [UIColor redColor];
//}

@end
